import query from './atom/db/query'; 
import count from './atom/db/count'; 
import { IBaseTable } from './atom/db/interfaces';
declare const action_id: string;
 declare const session_id: string; 
 declare const spaceid: string;
interface Message { 
    filter: {}; sort: {}; page_size: 1; page_no: 1;
 }
interface RecruitPlan extends IBaseTable {
    _id: string;
    work_plan: string;
    toem_ditch_name: string;
    toem_ditch_no: string;
    cost: string;
    start_time: number;
    finish_time: number;
    hir_nop: string;
    staus: string;
    audit_opinion: string;
    auditname: string;
    auditdate: number;
    instructions: string;
    memo: string;
    accessory_url: string;
    exec_plan_no: string;
}

function format(date: number) { 
    if (!date){
        return date;
    }
    const _date = new Date(parseFloat(date + '')); 
    let res = ''; if (_date) { res = _date.getFullYear() + '-' + _format(_date.getMonth() + 1) + '-' + _format(_date.getDate()); } return res;
 }
function _format(d: number) { if (d < 10) { return '0' + d; } else { return d; } }
async function atom(msg: Message) {
    const systemid = '6051'; const { exec, prepare } = query(action_id, session_id, spaceid, systemid);
     const asc: string[] = []; const desc: string[] = [];
      if (msg.sort) { const key = Object.keys(msg.sort)[0]; 
        if (msg.sort[key] === -1) { 
            desc.push(key); 
        } else { 
            asc.push(key); 
        } 
    }    // 教职工   
     prepare('recruit_plan', ['_id','work_plan','toem_ditch_name','toem_ditch_no','cost','start_time','finish_time','hir_nop','staus','audit_opinion','auditname','auditdate','instructions','memo','accessory_url','exec_plan_no'], msg.filter, msg.page_size, msg.page_no, asc, desc);   
      const [data] = (await exec()) as [RecruitPlan[]];

      const dataArray = data.map((it)=>{
          return Object.assign(it, {
            start_time: format(it.start_time),
            finish_time: format(it.finish_time),
            auditdate: format(it.auditdate)
          });
      });

    const c = count(action_id, session_id, spaceid, systemid); 
    c.prepare('recruit_plan', msg.filter); 
    const [t] = await c.exec();
    return { dataArray, count: t };
}